<template>
  <el-container style="padding: 15px">
    <el-header height="120px">
      <el-form size="small" label-width="70px">
        <el-row type="flex" :gutter="20" align="middle">
          <el-col :span="6">
            <el-form-item label="项目编号">
              <el-input v-model="searchObj.reportId" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目名称">
              <el-input v-model="searchObj.reportName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="产权人">
              <el-input v-model="searchObj.reportPropertyOwner" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="估价员">
              <el-select v-model="searchObj.valuerUserId" style="width: 100%" clearable filterable>
                <el-option v-for="item in valuerUserList" :key="item.id" :value="item.id" :label="'账号：' + item.account + '；昵称：' + item.nickName" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" :gutter="20" align="middle">
          <el-col :span="6">
            <el-form-item label="是否收费">
              <el-select v-model="searchObj.charge" style="width: 100%" clearable filterable>
                <el-option :value="true" :label="'是'" />
                <el-option :value="false" :label="'否'" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="票据情况">
              <el-select v-model="searchObj.billDetails" style="width: 100%" clearable filterable>
                <el-option :value="1" :label="'发票'" />
                <el-option :value="2" :label="'收据'" />
                <el-option :value="3" :label="'无票'" />
                <el-option :value="4" :label="'未填写'" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="审核状态">
              <el-select v-model="searchObj.leafId" multiple>
                <el-option v-for="item in leafList" :key="item.id" :label="item.leafName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="收费时间">
              <el-date-picker
                v-model="searchObj.payTime"
                value-format="timestamp"
                type="date"
                placeholder="开始时间"
                @change="startTimePay"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-date-picker
                v-model="searchObj.endPayTime"
                value-format="timestamp"
                type="date"
                placeholder="结束时间"
                @change="EndTimePay"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" :gutter="20" align="middle" justify="center">
          <el-col :span="4">
            <el-form-item label-width="0px">
              <el-button type="primary" @click="getPage">搜索</el-button>
              <el-button @click="clearSearch">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-divider />
    <el-main>
      <el-form size="small">
        <el-row type="flex">
          <el-col :span="4">
            <el-form-item>
              <el-button v-if="hasRole(['admin', 'statisticsDownload'])" type="primary" :loading="downloadingButtonLoding" @click="download">下载</el-button>
              <!--              <el-button type="warning" @click="tableHeadSetDialogVisible = true">表头设置</el-button>-->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table v-loading="listLoading" :data="financeList">
        <el-table-column label="项目编号" prop="reportId" width="120px">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="toDetails(scope.row.id)">{{ scope.row.reportId }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" prop="reportName" width="300px" :formatter="subName" />
        <el-table-column label="估价员" prop="valuerUserName" />
        <el-table-column label="产权人" prop="reportPropertyOwner" />
        <el-table-column label="业务员" prop="businessUserName" />
        <el-table-column label="已收费（万）" prop="actualCharge" />
        <el-table-column label="收费时间" prop="payTime" :formatter="tableTimeFormat" />
        <el-table-column label="开票金额（万）" prop="billCharge" />
        <el-table-column label="票据情况" prop="billDetails" align="bill">
          <template scope="bill">
            <p v-if="bill.row.billDetails == '0'">未填写</p>
            <p v-if="bill.row.billDetails == '1'">发票</p>
            <p v-if="bill.row.billDetails == '2'">收据</p>
            <p v-if="bill.row.billDetails == '3'">无票</p>
          </template>
        </el-table-column>
        <el-table-column label="开票时间" prop="billTime" :formatter="tableTimeFormat" />
        <!--        <el-table-column label="操作">-->
        <!--          <template>-->
        <!--            <el-button type="text" size="small">1</el-button>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-main>
  </el-container>
</template>

<script>
import { searchFinance } from '@/api/finance'
import { ellipsisString, parseTime } from '@/utils'
import { validObj } from '@/utils/validate'
import { downloadFinance } from '@/api/finance'
import { mapGetters } from 'vuex'
import { getSearchFormList } from '@/api/statistics'

export default {
  name: 'Index',
  data() {
    return {
      currentPage: 1,
      sizePage: 10,
      totalPage: 0,

      financeList: [],
      leafList: [],
      valuerUserList: [],

      searchObj: {},

      listLoading: false,

      downloadingButtonLoding: false
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'roles'
    ])
  },
  created() {
    this.init()
  },

  methods: {
    handleSizeChange(val) {
      this.sizePage = val
      this.getPage()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getPage()
    },

    init() {
      this.getPage()
      this.getSearchForm()
    },

    getPage() {
      const currentPage = this.currentPage
      const sizePage = this.sizePage
      const searchObj = this.searchObj
      this.listLoading = true

      searchFinance(currentPage, sizePage, searchObj).then(res => {
        this.financeList = res.data.list
        this.totalPage = res.data.total
      }).finally(() => {
        this.listLoading = false
      })
    },

    getSearchForm() {
      getSearchFormList().then(res => {
        this.valuerUserList = res.data.users
        this.leafList = res.data.leaf
      })
    },

    toDetails(id) {
      this.$router.push({ path: '/report/details', query: { id: id }})
    },

    clearSearch() {
      this.searchObj = {}
      this.getPage()
      // 重置成功消息框
      this.$message({
        message: '清除成功',
        type: 'success'
      })
    },
    hasRole(role) {
      const roles = this.roles
      const filter = roles.filter(v => role.includes(v))
      return filter.length !== 0
    },

    tableTimeFormat(row, column, cellValue, index) {
      return parseTime(cellValue, '{y}-{m}-{d}')
    },

    subName(row, column, cellValue, index) {
      return ellipsisString(cellValue)
    },

    download() {
      const searchObj = this.searchObj
      if (validObj(searchObj)) {
        this.downloadingButtonLoding = true
        downloadFinance(searchObj).then(res => {
          const url = window.URL.createObjectURL(new Blob([res]))
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', '收费表单.xlsx')
          document.body.appendChild(link)
          link.click()
          this.downloadingButtonLoding = false
        }).finally(() => {
          this.downloadingButtonLoding = false
        })
      } else {
        this.$message.warning('搜索信息不能为空')
      }
    },
    startTimePay(val) {
      this.reportCommonObj.payTime = val
    },
    EndTimePay(val) {
      this.reportCommonObj.endPayTime = val
    }
  }
}
</script>

<style scoped>

</style>
